package com.tengju.bff.interfaces.covercharge.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tengju.bff.interfaces.covercharge.enums.ProfitTypeEnum;
import com.tengju.bff.interfaces.covercharge.dto.QueryManageDetailReqDTO;
import com.tengju.bff.interfaces.covercharge.dto.ServiceManageDetailDTO;
import com.tengju.bff.interfaces.covercharge.entity.po.ServiceManageDetail;
import com.tengju.bff.interfaces.covercharge.entity.vo.AdminServiceManageQueryVo;
import com.tengju.bff.interfaces.covercharge.mapper.ServiceManageDetailMapper;
import com.tengju.bff.interfaces.covercharge.service.IServiceManageDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhaoyi
 * @since 2020-02-24
 */
@Service
public class ServiceManageDetailServiceImpl extends ServiceImpl<ServiceManageDetailMapper, ServiceManageDetail>
        implements IServiceManageDetailService {

    @Autowired
    private ServiceManageDetailMapper serviceManageDetailMapper;

    @Override
    public Page<AdminServiceManageQueryVo> queryPageByIdCode(Page page, String idCode, String startDate, String endDate,
                                                             String orderId, String otherId, String des, String profitType) {
        page.setRecords(addOtherId(serviceManageDetailMapper
                .queryPageByIdCode(page, idCode, startDate, endDate, orderId, otherId, des, profitType)));
        return page;
    }

    /**
     * @return java.util.List<com.tengju.bff.interfaces.covercharge.entity.vo.AdminServiceCustomQueryVo>
     * @Author xx
     * @Description 填充其它信息id
     * @Date: 2020/7/16 17:24
     * @Param []
     **/
    public List<AdminServiceManageQueryVo> addOtherId(List<AdminServiceManageQueryVo> list) {
        for (AdminServiceManageQueryVo adminServiceManageQueryVo : list) {
            //1.根据收益支出类型判断
            int type = adminServiceManageQueryVo.getProfitType();
            if (type == ProfitTypeEnum.pf_in.getCode() || type == ProfitTypeEnum.pf_out.getCode()) {
                String otherId = "订单id:" + adminServiceManageQueryVo.getOrderId() + ";商品id:" +
                        (adminServiceManageQueryVo.getGoodsId() != null ? adminServiceManageQueryVo.getGoodsId() : "无");
                adminServiceManageQueryVo.setOtherId(otherId);
                continue;
            }
            if (type == ProfitTypeEnum.jy_in.getCode() || type == ProfitTypeEnum.jy_out.getCode()) {
                String otherId = "订单id:" + adminServiceManageQueryVo.getOrderId() + ";商品id:" +
                        (adminServiceManageQueryVo.getGoodsId() != null ? adminServiceManageQueryVo.getGoodsId() : "无");
                adminServiceManageQueryVo.setOtherId(otherId);
                continue;
            }
            if (type == ProfitTypeEnum.ls_withdraw.getCode() || type == ProfitTypeEnum.ls_withdraw_refund.getCode()) {
                adminServiceManageQueryVo.setOtherId("乐税提现id:" + adminServiceManageQueryVo.getTableId());
                continue;
            }
            if (type == ProfitTypeEnum.self_withdraw.getCode() || type == ProfitTypeEnum.self_withdraw_refund
                    .getCode()) {
                adminServiceManageQueryVo.setOtherId("自认证提现id:" + adminServiceManageQueryVo.getTableId());
                continue;
            }
            if (type == ProfitTypeEnum.return_pf.getCode()) {
                String otherId = "退款id:" + (adminServiceManageQueryVo.getReturnId() == null ? "无" : adminServiceManageQueryVo.getReturnId()) +
                        ";订单id:" + (adminServiceManageQueryVo.getOrderId() == null ? "无" : adminServiceManageQueryVo.getOrderId());
                adminServiceManageQueryVo.setOtherId(otherId);
                continue;
            }
            if (type == ProfitTypeEnum.return_jy.getCode()) {
                String otherId = "退款id:" + (adminServiceManageQueryVo.getReturnId() == null ? "无" : adminServiceManageQueryVo.getReturnId())+
                ";订单id:" + (adminServiceManageQueryVo.getOrderId() == null ? "无" : adminServiceManageQueryVo.getOrderId());
                adminServiceManageQueryVo.setOtherId(otherId);
            }
        }
        return list;
    }

    /**
     * 根据IDCode和表id获取客戶明細
     */
    @Override
    public List<ServiceManageDetailDTO> getListFromIdCodeAndTableIdList(QueryManageDetailReqDTO req) {
        List<ServiceManageDetailDTO> lists = this.baseMapper.getListFromIdCodeAndTableIdList(req.getIdCode(),
                req.getTableIdList(), req.getOrderId(), Objects.isNull(req.getProfitType()) ? null : req.getProfitType().getCode());
        return lists;
    }

    /**
     * 获取客戶明細
     */
    @Override
    public List<ServiceManageDetailDTO> getManageDetailList(QueryManageDetailReqDTO req) {
        List<ServiceManageDetailDTO> lists = this.baseMapper.getManageDetailList(req.getIdCode(),
                req.getTableIdList(), req.getOrderId(), req.getDes(),
                Objects.isNull(req.getProfitType()) ? null : req.getProfitType().getCode(), req.getCreatedAt(), req.getUpdatedAt());
        return lists;
    }
}
